<template>
  <el-dialog :visible.sync="currentShowFlag" width="500px" append-to-body :close-on-click-modal="false" :close-on-press-escape="false" @open="handleDialogOpen" @closed="handleDialogClosed">
    <div slot="title" class="detail-dialog__header">数据加锁/解锁配置</div>
    <simple-form ref="form" v-model="formModel" label-width="100px" :form-field="formField" :grid="{span: 24}"></simple-form>
    <div slot="footer">
      <el-button @click="handleDialogClose">{{ $t('operation.close')}}</el-button>
      <el-button type="success" @click="handleChangeLockStatus('0')">解锁</el-button>
      <el-button type="danger" @click="handleChangeLockStatus('1')">加锁</el-button>
    </div>
  </el-dialog>
</template>
<script>
const BASEURL = '/Web/BizController/ajax.html'
export default {
  name: 'lockstatus',
  props: {
    showFlag: { type: Boolean, default: false },
    extData: { type: Object }
  },
  data () {
    return {
      formModel: {}
    }
  },
  inject: ['handleDataSearch'],
  computed: {
    formField () {
      return [
        {
          prop: 'uploadyear',
          label: '年份',
          type: 'Datepicker',
          component: { clearable: false, type: 'year', valueFormat: 'yyyy' }
        },
        {
          prop: 'uploadquarter',
          label: '季度',
          type: 'Select',
          component: { clearable: false, optionList: this.$getDictList('季度') }
        }
      ]
    },
    currentShowFlag: {
      get () { return this.showFlag },
      set (val) { this.$emit('update:showFlag', val) }
    }
  },
  methods: {
    handleDialogClose () {
      this.currentShowFlag = false
    },
    handleChangeLockStatus (value) {
      let loadingFlag = this.$loading({ target: this.$el.querySelector('.el-dialog') })
      let submitModel = this.$_.cloneDeep(this.formModel)
      submitModel = Object.assign({}, { sys_search: submitModel }, { sys_mid: this.extData.sys_mid })
      submitModel.lockStatus = value
      this.$axios
        .post(`${BASEURL}?sys_method=${this.extData.methodName}&sys_objName=${this.extData.objName}&sys_tableName=${this.extData.objName}`, submitModel)
        .then(resp => {
          this.currentShowFlag = false
          this.$message({ type: 'success', message: value === '1' ? '加锁成功' : '解锁成功' })
          this.handleDataSearch()
        })
        .finally(() => {
          loadingFlag.close()
        })
    },
    handleDialogOpen () {
      this.formModel = this.$_.cloneDeep(this.extData.searchModel)
    },
    handleDialogClosed () {
      this.$refs.form.resetFields()
      this.formModel = {}
    }
  }
}
</script>
